<?php

/**
 * test case for uTpl class
 */
class TestUtpl extends UnitTestCase {

    function testNoVars() {
        $str='This is a no vars template';
        $this->_testRendering($str,array(),$str);
    }

    function testPureVars() {
        // one var in the middle
        $tpl=new uTpl('Hello {{who}} !');
        $this->assertEqual($tpl->render(array('who'=>'World')),'Hello World !');
        $this->assertEqual($tpl->render(array('who'=>'to the red Moon')),'Hello to the red Moon !');
        $this->assertEqual($tpl->render(array('who'=>null)),'Hello  !');

        // two vars at the borders
        $tpl=new uTpl('{{start}} some text {{end}}');
        $this->assertEqual(
            $tpl->render(array('start'=>'ceci est un debut','end'=>'ceci est la fin'))
            ,'ceci est un debut some text ceci est la fin');

        // repeating var, complex name
        $tpl=new uTpl('one {{z_r0}} two {{z_r0}} three');
        $this->assertEqual(
            $tpl->render(array('z_r0'=>'Zero'))
            ,'one Zero two Zero three');
    }

    function testNull() {
        // $this->assertEqual();
    }

    function testTypes() {
        // integer
        $tpl=new uTpl('number is {{number}}');
        $this->assertEqual($tpl->render(array('number'=>36)),'number is 36');
    }

    function testRealCases() {
        $tpl=new uTpl("corbeille='0'
    {AND annee={anneeref}}");
        $result="corbeille='0'
    AND annee=2008";
        $this->assertEqual($tpl->render(array('anneeref'=>2008)),$result);

    }

    function testConditional() {
        $tpl=new uTpl('Hello{ nice {who} !}');
        $this->assertEqual(
            $tpl->render(array('who'=>null))
            ,'Hello');
        $this->assertEqual(
            $tpl->render(array('who'=>'people'))
            ,'Hello nice people !');

        // booleans
        $tpl=new uTpl('start {this {bool} is true} end');
        $this->assertEqual(
            $tpl->render(array('bool'=>true))
            ,'start this  is true end');
        $this->assertEqual(
            $tpl->render(array('bool'=>false))
            ,'start  end');
    }

    function testConstants() {
        $tpl=new uTpl('UNDEFINED={{UNDEFINED}}');
        $this->assertEqual(
            $tpl->render(),'UNDEFINED='
            ,"undefined constants should render empty string");

        define('DEFINED_FOR_uTPL_UNIT_TESTS','present !');
        // echo DEFINED_FOR_uTPL_UNIT_TESTS;
        $tpl=new uTpl('DEFINED={{DEFINED_FOR_uTPL_UNIT_TESTS}}');
        $this->assertEqual(
            $tpl->render(),'DEFINED=present !'
            ,"defined constants should render their value %s");
        $this->assertEqual(
            $tpl->render(array('DEFINED_FOR_uTPL_UNIT_TESTS'=>'var !'))
            ,'DEFINED=var !'
            ,"but constants should be overrided by parameters if provided");
    }

    function testSource() {
        $src='Hello {{who}} !';
        $tpl=new uTpl($src);
        /*if (!$this->assertTrue(
            in_array('getsource',get_class_methods('uTpl'))
            ,'uTpl should have a getSource() method'))
            return false;*/
        $this->assertEqual(
            $tpl->getSource(),$src
            ,"getSource should return source");
        $this->assertEqual(
            $tpl->render(array('who'=>'world'))
            ,"Hello world !");
        $this->assertEqual(
            $tpl->getSource(),$src
            ,"source should not change after rendering");
        /*if (!$this->assertTrue(
            in_array('setsource',get_class_methods('uTpl'))
            ,'uTpl should have a setSource() method'))
            return false;*/
        $new = "Welcome {{who}} !";
        $this->assertTrue(
            $tpl->setSource($new)
            ,"setSource() should return true");
        $tpl->render(array('who'=>'world'));
        $this->assertEqual(
            $tpl->getSource(),$new
            ,"setSource should have changed source");
        $this->assertEqual(
            $tpl->render(array('who'=>'John'))
            ,"Welcome John !"
            ,"setSource should have REALLY changed source");
    }

    /**
     * test rendering
     * @param string $str source template
     * @param array $data vars to inject
     * @param string $result expected result
     */
    function _testRendering($str,$data,$result) {
        $tpl=new uTpl($str);
        $this->assertEqual($tpl->render($data),$result);
    }
}
